#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Drawing;
using System.Xml.Linq;
using System.ComponentModel;
using System.Globalization;
using System.Diagnostics;

namespace EngineOfEvermore.Common
{
    public static class FloatSerializer
    {
        public static String Serialize( float value )
        {
            return value.ToString( CultureInfo.InvariantCulture.NumberFormat );
        }

        public static float Deserialize( String value )
        {
            return float.Parse( value, CultureInfo.InvariantCulture.NumberFormat );
        }

        public static XElement Serialize( float value, XName elementName )
        {
            return new XElement( elementName, Serialize( value ) );
        }

        public static float Deserialize( XElement element )
        {
            Debug.Assert( !element.HasElements );
            return Deserialize( element.Value );
        }
    }

    public static class IntSerializer
    {
        public static String Serialize( int value )
        {
            return value.ToString( CultureInfo.InvariantCulture.NumberFormat );
        }

        public static int Deserialize( String value )
        {
            return int.Parse( value, CultureInfo.InvariantCulture.NumberFormat );
        }

        public static XElement Serialize( int value, XName elementName )
        {
            return new XElement( elementName, Serialize( value ) );
        }

        public static int Deserialize( XElement element )
        {
            Debug.Assert( !element.HasElements );
            return Deserialize( element.Value );
        }
    }
	
	public class RectangleFXmlSerializer : IXmlObjectSerializer
	{
		RectangleF _obj;
		
		public Object Obj 
		{ 
			get {
				return _obj; 
			}
			
			set {
				if ( value is RectangleF ) {
					_obj = (RectangleF) value;
				} 
			}
		}
		
			
		public void getXml( XElement node, XmlSerialization serializer )
		{
			if ( _obj != null ) {
                // I changed this to use a type converter instead of ToString(). Otherwise the
                // string representation is dependent on the local number format. -Michael
                var tc = TypeDescriptor.GetConverter( typeof( float ) );

				node.Add( new XElement( "X", tc.ConvertToInvariantString( _obj.X ) ) );
                node.Add( new XElement( "Y", tc.ConvertToInvariantString( _obj.Y ) ) );
				node.Add( new XElement( "Width", tc.ConvertToInvariantString( _obj.Width ) ) );
				node.Add( new XElement( "Height", tc.ConvertToInvariantString( _obj.Height ) ) );
			}
		}
		
		
		public Object loadFromXml( XElement node, XmlSerialization serializer )
		{
			_obj = new RectangleF( 0, 0, 0, 0 );
			if ( node.Element( "X" ) != null )
				_obj.X = float.Parse( node.Element( "X" ).Value, 
                    System.Globalization.NumberFormatInfo.InvariantInfo );
			
			if ( node.Element( "Y" ) != null )
                _obj.Y = float.Parse( node.Element( "Y" ).Value,
                    System.Globalization.NumberFormatInfo.InvariantInfo );
			
			if ( node.Element( "Width" ) != null )
                _obj.Width = float.Parse( node.Element( "Width" ).Value,
                    System.Globalization.NumberFormatInfo.InvariantInfo );
			
			if ( node.Element( "Height" ) != null )
                _obj.Height = float.Parse( node.Element( "Height" ).Value,
                    System.Globalization.NumberFormatInfo.InvariantInfo );
			
			return _obj;
		}
		
		
	}
	
	
}
